﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mapeo;
using NHibernate;
using BE;
using System.Data;
using MySql.Data.MySqlClient;
using NHibernate.Criterion;

namespace DAL
{
    public class EntradaCaja_DAL: Controlador
    {
        public void Insertar(EntradaCaja EntradaCaja) {
            using (ISession Sesion=m_SessionFactory.OpenSession())
            {
                using(ITransaction trans=Sesion.BeginTransaction())
                {
                    try 
	                {
                        Sesion.Save(EntradaCaja);
                        Sesion.Flush();

                        MySqlCommand SqlCmd = new MySqlCommand();
                        SqlCmd.Connection = (MySqlConnection)Sesion.Connection;
                        trans.Enlist(SqlCmd);

                        SqlCmd.CommandText = "sp_NumeracionDocumentos";
                        SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                        SqlCmd.Parameters.AddWithValue("@in_IDTipoDocumento", EntradaCaja.TipoDocumento.IDTipoCp);
                        SqlCmd.Parameters.AddWithValue("@in_IDCp", EntradaCaja.IDCp);
                        SqlCmd.Parameters.AddWithValue("@in_Tabla", EntradaCaja.TipoDocumento.Entidad);
                        SqlCmd.ExecuteNonQuery();



                        SqlCmd.CommandText = "sp_ActualizarSaldoCaja";
                        SqlCmd.Parameters.Clear();
                        SqlCmd.Parameters.AddWithValue("@in_IDCajaDiaria", EntradaCaja.IDCajaDiaria);
                        SqlCmd.Parameters.AddWithValue("@in_Monto", EntradaCaja.Total);
                        SqlCmd.Parameters.AddWithValue("@in_Factor", 1);
                        SqlCmd.Parameters.Add("@out_Error", MySqlDbType.String);
                        SqlCmd.Parameters["@out_Error"].Direction = ParameterDirection.Output;
                        SqlCmd.ExecuteNonQuery();

                        String resultadoAC = Convert.ToString(SqlCmd.Parameters["@out_Error"].Value);
                        trans.Commit();
	                }
	                catch (Exception)
	                {
                        trans.Rollback();
		                throw;
	                }
                }
            }
        }
        public List<TipoDocumentoCaja> ObtenerTipoDocumentos()
        {
            try
            {
                using (ISession Sesion = m_SessionFactory.OpenSession())
                {
                    ICriteria criteria = Sesion.CreateCriteria("TipoDocumentoCaja").Add(Restrictions.Eq("TipoMovimiento","Entrada"))
                        .AddOrder(Order.Asc("Nombre"));
                    //criteria.SetMaxResults(100);
                    List<TipoDocumentoCaja> listado = (List<TipoDocumentoCaja>)criteria.List<TipoDocumentoCaja>();
                    return listado;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable ObtenerCajaDiarias()
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    MySqlCommand SqlCmd = new MySqlCommand();
                    SqlCmd.Connection = (MySqlConnection)Sesion.Connection;
                    Trans.Enlist(SqlCmd);
                    SqlCmd.CommandText = "sp_ObtenerCajaDiariaActivas";
                    SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlCmd.ExecuteNonQuery();
                    MySqlDataAdapter da = new MySqlDataAdapter(SqlCmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    Trans.Commit();
                    return dt;
                }
            }
        }
        public SocioNegocio ObtenerSocioNegocioxCodigo(String id)
        {
            try
            {
                return (SocioNegocio)AyudaNHibernate.ObtenerEntidadPorEtiqueta("SocioNegocio","Codigo", id);
            }
            catch (Exception)
            {

                throw;
            }
        
        }
        public DataTable BuscarSocioNegocio(String Nombre)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    MySqlCommand SqlCmd = new MySqlCommand();
                    SqlCmd.Connection = (MySqlConnection)Sesion.Connection;
                    Trans.Enlist(SqlCmd);
                    SqlCmd.CommandText = "sp_BuscarSocioNegocio";
                    SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlCmd.Parameters.AddWithValue("@in_Nombre", Nombre);

                    SqlCmd.ExecuteNonQuery();
                    MySqlDataAdapter da = new MySqlDataAdapter(SqlCmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    Trans.Commit();
                    return dt;
                }
            }
        }


        public DataTable listado(String Numeracion, String Cliente)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    MySqlCommand SqlCmd = new MySqlCommand();
                    SqlCmd.Connection = (MySqlConnection)Sesion.Connection;
                    Trans.Enlist(SqlCmd);
                    SqlCmd.CommandText = "sp_ObtenerListado_EntradaCaja";
                    SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlCmd.Parameters.AddWithValue("@inNumeracion", Numeracion);
                    SqlCmd.Parameters.AddWithValue("@inCliente", Cliente);


                    SqlCmd.ExecuteNonQuery();
                    MySqlDataAdapter da = new MySqlDataAdapter(SqlCmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    Trans.Commit();
                    return dt;
                }
            }
        }


        public DataTable listadoCpxCaja(String idCajaDiaria)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    MySqlCommand SqlCmd = new MySqlCommand();
                    SqlCmd.Connection = (MySqlConnection)Sesion.Connection;
                    Trans.Enlist(SqlCmd);
                    SqlCmd.CommandText = "sp_ListadoEntradaCaja";
                    SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlCmd.Parameters.AddWithValue("@in_idCajaDiaria", idCajaDiaria);

                    SqlCmd.ExecuteNonQuery();
                    MySqlDataAdapter da = new MySqlDataAdapter(SqlCmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    Trans.Commit();
                    return dt;
                }
            }
        }

    }
}
